package com.yangming.springboot.deepin.playTest;

import com.yangming.springboot.deepin.playTest.ThreadPool;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @创建人 ym
 * @创建时间 2021/4/8 0008
 * @描述
 **/
public class DefaultThreadPool<Job extends Runnable> implements ThreadPool<Job> {

    private static final int MAX_WORKER_NUMBERS = 10;
    private static final int DEFAULT_WORKER_NUMBERS = 5;
    private static final int MIN_WORKER_NUMBERS = 1;
    private final List<Worker> workers = Collections.synchronizedList(new ArrayList<Worker>());
    private int workerNum = DEFAULT_WORKER_NUMBERS;
    private AtomicLong threadNum = new AtomicLong();
    private final LinkedList<Job> jobs = new LinkedList<Job>();

    public DefaultThreadPool(){
        initializeWorkers(DEFAULT_WORKER_NUMBERS);
    }

    public DefaultThreadPool(int num){
        workerNum = num > MAX_WORKER_NUMBERS ? MAX_WORKER_NUMBERS : (num < MIN_WORKER_NUMBERS ? MIN_WORKER_NUMBERS : num);
        initializeWorkers(num);
    }

    public void execute(Job job){
        if (job != null){
            synchronized(jobs){
                jobs.addLast(job);
                jobs.notify();
            }
        }
    }

    public void shutDown(){
        for (Worker worker : workers){
            worker.shutdown();
        }
    }

    public void addWorkers(int num){
        synchronized(jobs){
            if (num + this.workerNum > MAX_WORKER_NUMBERS){
                num = MAX_WORKER_NUMBERS - this.workerNum;
            }
            initializeWorkers(num);
            this.workerNum += num;
        }
    }

    public void removeWorker(int num){
        synchronized (jobs){
            if (num >= this.workerNum){
                throw new IllegalArgumentException("beond workNum");
            }
            int count = 0;
            while(count < num){
                Worker worker = workers.get(count);
                if (workers.remove(worker)){
                    worker.shutdown();
                    count++;
                }
            }
            this.workerNum -= count;
        }
    }

    public int getJobSize(){
        return jobs.size();
    }

    private void initializeWorkers(int num){
        for (int i=0; i<num; i++){
            Worker worker = new Worker();
            workers.add(worker);
            Thread thread = new Thread(worker, "threadPool-worker-" + threadNum.incrementAndGet());
            thread.start();
        }
    }

    private class Worker implements Runnable{
        private volatile boolean running = true;
        @Override
        public void run(){
            while(running){
                Job job = null;
                synchronized(jobs){
                    while(jobs.isEmpty()){
                        try{
                            jobs.wait();
                        }catch(InterruptedException ex){
                            Thread.currentThread().interrupt();
                            return;
                        }
                    }
                    job = jobs.removeFirst();
                }
                if (job != null){
                    try{
                        job.run();
                    }catch(Exception ex){
                        ex.printStackTrace();
                    }
                }
            }
        }

        public void shutdown(){
            running = false;
        }
    }

}
